package com.shunbo.convenient.utils;

public class MapUtil {
	
	public static double MapResult(String address,String lat,String lng){
		 String doGet = HttpClientUtil.doGet("http://api.map.baidu.com/geocoder/v2/?ak=xrG8zYfmNIQD0kxTMMzPooX7dkppD5Am&location="+lat+","+lng+"&output=json");
		 String url = HttpClientUtil.doGet("http://api.map.baidu.com/geocoder/v2/?address="+address+"&output=json&ak=xrG8zYfmNIQD0kxTMMzPooX7dkppD5Am");
		 System.out.println(url);
		System.out.println(doGet);
		 MapResult pojo = JsonUtils.jsonToPojo(url, MapResult.class);
		 ApiResult pojo1 = JsonUtils.jsonToPojo(doGet, ApiResult.class);
	       
		 System.out.println(pojo);
		 System.out.println(pojo1);
		 return Distance(pojo.getResult().getLocation().getLng(),pojo.getResult().getLocation().getLat(),pojo1.getResult().getLocation().getLng(),pojo1.getResult().getLocation().getLat());
	}
	public static Results MapLnglat(String lat, String lng){
		 String doGet = HttpClientUtil.doGet("http://api.map.baidu.com/geocoder/v2/?ak=xrG8zYfmNIQD0kxTMMzPooX7dkppD5Am&location="+lat+","+lng+"&output=json&pois=0");
		 System.out.println(doGet);
		 ApiResult pojo1 = JsonUtils.jsonToPojo(doGet, ApiResult.class);
		 System.out.println(pojo1);
		 return pojo1.getResult();
	}

	   public static double Distance(double long1, double lat1, double long2,
	            double lat2) {  
	        double a, b, R;  
	        R = 6378137; // ����뾶  
	        lat1 = lat1 * Math.PI / 180.0;  
	        lat2 = lat2 * Math.PI / 180.0;  
	        a = lat1 - lat2;  
	        b = (long1 - long2) * Math.PI / 180.0;  
	        double d;  
	        double sa2, sb2;  
	        sa2 = Math.sin(a / 2.0);  
	        sb2 = Math.sin(b / 2.0);  
	        d = 2  
	                * R  
	                * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1)  
	                        * Math.cos(lat2) * sb2 * sb2));  
	        return d;  
	    }

	public static void main(String[] args) {
		//String s = HttpClientUtil.doGet("http://api.map.baidu.com/geocoder/v2/?ak=xrG8zYfmNIQD0kxTMMzPooX7dkppD5Am&location=39.90602111816406,116.49919891357422&output=json");
		Results results = MapLnglat(39.90602111816406 + "", 116.49919891357422 + "");
		System.out.println(results.getAddressComponent().getDistrict());
	}

}
